<template>
	<deptSelect class="width-full popover-reference" :selectMap="selectMap" v-bind="$attrs" @blur="blur" @show="filterDeptList" ref="field" :deptList="deptList" :disabled="disabled" :isFilter="isFilter" :value="value" @input="changeValue" :isCheckbox="item.selectType === 2 " :isService="item.dataType === 5" :fieldType="item.fieldType">
	</deptSelect>
</template>

<script lang="ts">
import { Component, Mixins, Inject } from 'vue-property-decorator';
import deptSelect from './components/deptSelect.vue';
import widgetCommon from '../common/widgetCommon';
import { CommonModule } from '@/store/modules/common'
import { busConfigStore } from '@/afterSales/views/config/serviceConfig/store/busConfigStore'
import { commonAction, handleData } from '@/utils/common'
// eslint-disable-next-line no-restricted-imports
import { intersection } from 'lodash';
@Component({
  name: 'department',
  components: {
    deptSelect
  }
})
export default class extends Mixins(widgetCommon) {
  @Inject({ default: {} }) private readonly recordVM!: any;
  private isFilter = false;
  private getPathIds (id) {
    const arr: number[] = [];
    const depMap = (CommonModule as any).publicCache.departmentListObj;
    let current = depMap[id] || {};
    while (current.parentId) {
      const pNode: any = depMap[current.parentId];
      if (pNode && pNode.parentId !== 0) {
        arr.unshift(Number(current.parentId));
        current = pNode;
      } else {
        break;
      }
    }
    return arr;
  }

  private get selectMap () {
    if (this.item.dataType === 2) {
      return (CommonModule as any).publicCache.departmentListObj
    } else if (this.item.dataType === 4) {
      return (CommonModule as any).publicCache.queueListObj
    } else {
      return busConfigStore.serviceMap
    }
  }

  private deptList: any = [];
  private filterDeptList () {
    const { dept = [], service = [], technology = [] } = this.item.optionalScope || {};
    this.isFilter = false;
    if (dept.length || service.length || technology.length) {
      this.isFilter = true;
      const arr: any = [];
      if (this.item.dataType === 2) {
        const departmentMap = (CommonModule as any).publicCache.departmentListObj;
        const allDep = [...new Set([...dept])];
        Object.keys(departmentMap).forEach((id) => {
          const path = this.getPathIds(id);
          if (intersection([...path, id], allDep).length) {
            const deptObj = departmentMap[id];
            if (deptObj) {
              arr.push({
                id: deptObj._id,
                _id: deptObj._id,
                name: deptObj.name,
                name1: path
                  .map((item) => {
                    return departmentMap[item].name;
                  })
                  .join('/')
              });
            }
          }
        });
        this.deptList = arr;
      } else if (this.item.dataType === 4) {
        this.deptList = ((CommonModule as any).publicCache.queueList || []).filter(item => {
          return technology.includes(item._id)
        });
      } else if (this.item.dataType === 5) {
        this.deptList = (busConfigStore.serviceList || []).filter(item => {
          return service.includes(item.id)
        });
      }
    } else {
      if (this.item.dataType === 2) { // 说明选择的是部门
        this.deptList = (CommonModule as any).publicCache.departmentTree?.tree || [];
      } else if (this.item.dataType === 4) { // 说明是技能组
        this.deptList = (CommonModule as any).publicCache.queueList || [];
      } else if (this.item.dataType === 5) {
        this.deptList = busConfigStore.serviceList || [];
      }
    }
  }

  private beforeMount () {
    if (!busConfigStore.serviceList.length) {
      commonAction({
        url: '/server-work/action/serviceGroup/listAll',
        method: 'post'
      }).then(res => {
        if (res.success) {
          handleData(res.data.serviceGroupList, 'service')
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped></style>
